﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using nERP.Model.Fin;
using nERP.Model;
using nERP.Model.Bas;

namespace nERP.ViewModel.Fin.PaymentVMs
{
    public partial class PaymentListVM : BasePagedListVM<Payment_View, PaymentSearcher>
    {

        protected override IEnumerable<IGridColumn<Payment_View>> InitGridHeader()
        {
            return new List<GridColumn<Payment_View>>{

                this.MakeGridHeader(x => x.Payment_PayCode).SetTitle(@Localizer["Page.付款单号"].Value),
                this.MakeGridHeader(x => x.Payment_Supplier).SetTitle(@Localizer["Page.供应商"].Value),
                this.MakeGridHeader(x => x.Payment_MakeUser).SetTitle(@Localizer["Page.制单人"].Value),
                this.MakeGridHeader(x => x.Payment_PayDate).SetTitle(@Localizer["Page.付款日期"].Value),
                this.MakeGridHeader(x => x.Payment_SettlementType).SetTitle(@Localizer["Page.结算方式"].Value),
                this.MakeGridHeader(x => x.Payment_SettlementAccount).SetTitle(@Localizer["Page.结算账户"].Value),
                this.MakeGridHeader(x => x.Payment_TotalPrice).SetTitle(@Localizer["Page.付款金额"].Value),
                this.MakeGridHeader(x => x.Payment_Status).SetTitle(@Localizer["Page.状态"].Value),
                this.MakeGridHeader(x => x.Payment_Remark).SetTitle(@Localizer["_Admin.Remark"].Value),
                this.MakeGridHeader(x => x.Payment_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.Payment_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.Payment_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.Payment_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),
                this.MakeGridHeader(x => x.Payment_IsValid).SetTitle(@Localizer["_Admin.IsValid"].Value),
                this.MakeGridHeader(x => "wtmtemp_CanApprove").SetHide().SetFormat((a, b) =>
                {
                    if(a.wtmtemp_CanApprove) { return "true"; }
                    else { return "false"; }
                }),


                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<Payment_View> GetSearchQuery()
        {
            var query = DC.Set<Payment>()

                .CheckContain(Searcher.PayCode, x => x.PayCode)
                .CheckEqual(Searcher.SupplierId, x => x.SupplierId)
                .CheckEqual(Searcher.MakeUserId, x => x.MakeUserId)
                .CheckEqual(Searcher.SettlementTypeId, x => x.SettlementTypeId)
                .CheckBetween(Searcher.PayDate?.GetStartTime(), Searcher.PayDate?.GetEndTime(), x => x.PayDate, includeMax: false)
                .CheckEqual(Searcher.SettlementAccountId, x => x.SettlementAccountId)
                .CheckEqual(Searcher.Status, x => x.Status)
                .CheckEqual(Searcher.TotalPrice, x => x.TotalPrice)
                .CheckContain(Searcher.Remark, x => x.Remark)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x => x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x => x.UpdateBy)
                .Select(x => new Payment_View
                {
                    ID = x.ID,

                    Payment_PayCode = x.PayCode,
                    Payment_Supplier = DC.Set<Supplier>().Where(z0 => z0.SupplierCode == x.SupplierId).Select(y => y.SupplierName).FirstOrDefault(),
                    Payment_MakeUser = DC.Set<FrameworkUser>().Where(z0 => z0.ITCode == x.MakeUserId).Select(y => y.Name).FirstOrDefault(),
                    Payment_PayDate = x.PayDate,
                    Payment_SettlementType = x.SettlementType.DicFieldDes,
                    Payment_SettlementAccount = DC.Set<SettlementAccount>().Where(z0 => z0.Code == x.SettlementAccountId).Select(y => y.Name).FirstOrDefault(),
                    Payment_Status = x.Status,
                    Payment_TotalPrice = x.TotalPrice,
                    Payment_Remark = x.Remark,
                    Payment_CreateTime = x.CreateTime,
                    Payment_UpdateTime = x.UpdateTime,
                    Payment_CreateBy = x.CreateBy,
                    Payment_UpdateBy = x.UpdateBy,
                    Payment_IsValid = x.IsValid,
                })
                .OrderBy(x => x.ID);
            return query;
        }


        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                var ids = GetMyApproves();
                foreach (var item in EntityList)
                {
                    var m = ids.Find(x => x.ModelID == item.GetID().ToString());
                    if (m != null)
                    {
                        item.wtmtemp_CanApprove = true;
                    }
                }
            }
        }


    }
    public class Payment_View : Payment
    {

        public string Payment_PayCode { get; set; }
        public string Payment_Supplier { get; set; }
        public string Payment_MakeUser { get; set; }
        public DateTime? Payment_PayDate { get; set; }
        public string Payment_SettlementType { get; set; }
        public string Payment_SettlementAccount { get; set; }
        public StatusTypeEnum? Payment_Status { get; set; }
        public decimal? Payment_TotalPrice { get; set; }
        public string Payment_Remark { get; set; }
        public DateTime? Payment_CreateTime { get; set; }
        public DateTime? Payment_UpdateTime { get; set; }
        public string Payment_CreateBy { get; set; }
        public string Payment_UpdateBy { get; set; }
        public bool Payment_IsValid { get; set; }
        public bool wtmtemp_CanApprove { get; set; }

    }

}